import Vue from 'vue'
import VueRouter from 'vue-router'
import axios from 'axios'
// import '../plugins/element.js'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Login')
  },
  {
    path: '/home',
    // name: 'home',
    component: () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Home.vue'),
    children: [
      { path: '/', redirect: 'welcome' },
      {
        path: 'welcome',
        name: 'welcome',
        component: () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Welcome')
      },
      {
        path: '/users',
        name: 'users',
        component: () => import(/* webpackChunkName:"Users_Rights_Roles" */ '../components/user/Users')
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import(/* webpackChunkName:"Users_Rights_Roles" */ '../components/power/Rights')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import(/* webpackChunkName:"Users_Rights_Roles" */ '../components/power/Roles')
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import(/* webpackChunkName:"Cate_Params" */ '../components/goods/Cate')
      },
      {
        path: '/params',
        name: 'params',
        component: () => import(/* webpackChunkName:"Cate_Params" */ '../components/goods/Params')
      },
      {
        path: '/goods',
        name: 'goodslist',
        component: () => import(/* webpackChunkName:"Goodslist_Add" */ '../components/goods/List')
      },
      {
        path: '/goods/add',
        name: 'goodsadd',
        component: () => import(/* webpackChunkName:"Goodslist_Add" */ '../components/goods/Add.vue')
      },
      {
        path: '/orders',
        name: 'orders',
        component: () => import(/* webpackChunkName:"Orders_Reports" */ '../components/order/Order.vue')
      },
      {
        path: '/reports',
        name: 'reports',
        component: () => import(/* webpackChunkName:"Orders_Reports" */ '../components/report/Report.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
/* 路由守卫
    to:将要访问的路径，
    from:从什么路径过来
    next 是一个函数，表示方形
    next('/login')强制跳转login路由
 */

router.beforeEach(async (to, from, next) => {
  // 如果跳转的路径是login登录页面则正常跳转
  if (to.path === '/login') return next()
  //   否则判断sessionStorage是否有token,如果没有token说明已经退出登录，则需要重新登录，跳转login页面
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  // 如果有token,但是token过期了，需要发起一次请求进行判断，如果返回的信息表示token失效的话则跳转login登录页面重新登录
  // 注意由于这里router还没有加载进vue,所以vue上绑定的this.$http请求还不能用，需要导入axios单独发起一次请求，根据返回的信息进行判断
  const { data: res } = await axios.get('menus')
  if (res.meta.msg === '无效token') {
    this.$message.error(res.meta.msg)
    return next('/login')
  }
  next()
})

export default router
